home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / Source / Foundation / OS / ZScrap.cpp < prev    next >
Text File  |  1997-06-18  |  3KB  |  126 lines

  1. /*
  2.  *  File:       ZScrap.cpp
  3.  *  Summary:       Some global functions that make it easier to deal with the Scrap Manager.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1996 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *  Change History (most recent first):
  10.  *
  11.  *         <->     7/22/96    JDJ        Created.
  12.  */
  13.  
  14. #include <ZScrap.h>
  15.  
  16. #include <Scrap.h>
  17.  
  18. #include <ZExceptions.h>
  19. #include <ZLocker.h>
  20.  
  21.  
  22. //---------------------------------------------------------------
  23. //
  24. // InScrap
  25. //
  26. //---------------------------------------------------------------
  27. bool InScrap(OSType type)
  28. {
  29.     long offset;
  30.     long length = GetScrap(nil, type, &offset);
  31.         
  32.     return length > 0;
  33. }
  34.  
  35.  
  36. //---------------------------------------------------------------
  37. //
  38. // GetScrap (OSType)
  39. //
  40. //---------------------------------------------------------------
  41. THandle    GetScrap(OSType type)
  42. {
  43.     THandle data;
  44.     
  45.     long offset;
  46.     OSErr length = (OSErr) GetScrap(data, type, &offset);
  47.     if (length < 0)
  48.         ThrowOSErr(length);
  49.         
  50.     return data;
  51. }
  52.  
  53.  
  54. //---------------------------------------------------------------
  55. //
  56. // PutScrap 
  57. //
  58. //---------------------------------------------------------------
  59. void PutScrap(OSType type, const THandle& data)
  60. {
  61.     {
  62.     TLocker lock(data);
  63.         OSErr err = (OSErr) ::PutScrap((long) data.GetSize(), type, (Ptr) data.GetPtr());
  64.         ThrowIfOSErr(err);
  65.     }
  66. }
  67.  
  68.  
  69. //---------------------------------------------------------------
  70. //
  71. // GetScrap ()
  72. //
  73. //---------------------------------------------------------------
  74. THandle    GetScrap()
  75. {
  76.     THandle data;
  77.     
  78.     // Make sure the scrap data is in memory.
  79.     long err = LoadScrap();
  80.     if (err == noErr) {
  81.     
  82.         // Get a pointer to the scrap info record.
  83.         ScrapStuff* scrap = InfoScrap();            
  84.         if (scrap->scrapSize > 0) {
  85.         
  86.             // Make a copy of the scrap handle.
  87.             Handle temp = scrap->scrapHandle;
  88.             OSErr err = HandToHand(&temp);
  89.             if (err == noErr) {
  90.             
  91.                 // Resize our copy so that it matches the scrapSize.
  92.                 SetHandleSize(temp, scrap->scrapSize);
  93.                 if (MemError() == noErr)
  94.                     data = temp;                // THandle will take mac handle                    
  95.                 else
  96.                     DisposeHandle(data);
  97.             }
  98.         }
  99.     }
  100.     
  101.     return data;
  102. }
  103.  
  104.  
  105. //---------------------------------------------------------------
  106. //
  107. // SetScrap
  108. //
  109. //---------------------------------------------------------------
  110. void SetScrap(const THandle& oldScrap)
  111. {
  112.     if (oldScrap.GetSize() > 0) {
  113.         ::ZeroScrap();
  114.         
  115.         Handle temp = oldScrap;
  116.         OSErr err = HandToHand(&temp);
  117.         if (err == noErr) {
  118.             ScrapStuff* scrap = InfoScrap();    
  119.             scrap->scrapHandle = temp;
  120.             scrap->scrapSize   = GetHandleSize(temp);
  121.         }
  122.     }
  123. }
  124.  
  125.  
  126.